package com.wframe.project.jnd.wx.controller;

import com.frame.database.JdataBean;
import com.frame.database.PageResult;
import com.wframe.project.jnd.rest.util.JsonReturn;
import com.wframe.project.jnd.services.InformationService;
import com.wframe.project.jnd.util.WebUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/18.
 */
@Controller
@RequestMapping("/we/article")
public class WeInformationController {

    @Autowired
    private InformationService informationService;

    @RequestMapping("/list")
    public String info(){
        return "/wesite/view/article/list";
    }
    @RequestMapping("/customer")
    public String customer(){
        return "/wesite/view/article/customer";
    }
    /**
     * 加载微信文章列表
     * @param size
     * @param pno
     * @return
     */
//    @RequestMapping(value = "info")
//    public String info(String code, Model model) {
//        List<JdataBean> list = informationService.getIssueByCode(code);
//        List<Map> dlist = new ArrayList<>();
//        for(JdataBean bean:list){
//            Map map = new HashMap();
//            map.putAll(bean.getDataMap());
//            dlist.add(map);
//        }
//        model.addAttribute("list",dlist);
//        return "/wesite/view/article/list";
//
//    }
    @RequestMapping(value = "info")
    @ResponseBody
    public String info(HttpServletResponse response, String code, int pno, Model model, Integer size) {
        JsonReturn json = new JsonReturn();
        try {
            JdataBean bean = new JdataBean();
            bean.put("code",code);
            PageResult pr = informationService.wxqueryInfos(bean, 15, pno);

            json.putData(pr.getList());
        }catch (Exception e){
            return json.toErrorString(this.getClass(), e);
        }
        return json.toString();
    }
    /**
     * 加载文章信息详情
     *
     * @return
     */
    @RequestMapping(value = "details")
    public String details(String id, String code, Model model) {
        JdataBean bean = null;
        if (StringUtils.isNotBlank(id)) {
            bean = informationService.getInfo(id);
        } else if (StringUtils.isNotBlank(code)) {
            bean = informationService.getInfoByCode(code);
        }
        if (bean != null) {
            id = bean.getString("id");
            JdataBean channel = informationService.getChannel(bean.getString("code"));
            bean.put("content", informationService.getInfoCon(id));
            model.addAttribute("info", bean.getDataMap());
            model.addAttribute("channeltitle", channel.getString("name"));
        }
        return "/wesite/view/article/details";
    }

    /**
     * 获取用户协议详情
     *
     * @return
     */
    @RequestMapping(value = "agreement")
    public String agreement(String id, String code, Model model) {
        JdataBean bean = null;
        id="47";
        if (StringUtils.isNotBlank(id)) {
            bean = informationService.getInfo(id);
        } else if (StringUtils.isNotBlank(code)) {
            bean = informationService.getInfoByCode(code);
        }
        if (bean != null) {
            id = bean.getString("id");
            JdataBean channel = informationService.getChannel(bean.getString("code"));
            bean.put("content", informationService.getInfoCon(id));
            model.addAttribute("info", bean.getDataMap());
            model.addAttribute("channeltitle", channel.getString("name"));
        }
        return "/wesite/view/article/agreement";
    }
}
